<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;

class HomeworkStudentController extends BaseController {

	public function index() {
		if(IS_POST) {
			$homeworkId = I('post.homework_id');
			$homework = D('Homework')
				->where(array('id' => $homeworkId))
				->relation('logic_class')
				->find();
		} else {
			$homework = D('Homework')->relation('logic_class')->find();
			$homeworkId = $homework['id'];
		}
		$logicClassId = $homework['logic_class']['id'];
		$homeworks = M('Homework')
			->where(array('logic_class_id' => $logicClassId))
			->order('start_time')
			->select();
		$this->assign('logic_class_id', $logicClassId);
		$this->assign('logic_classes', M('LogicClass')->select());
		$this->assign('homework_id', $homeworkId);
		$this->assign('homeworks', $homeworks);
		$studentIds = M('LogicClassStudent')
			->where(array('logic_class_id' => $logicClassId))
			->getField('student_id', true);
		$map['id'] = array('in', $studentIds);
		$studentIds && $students = D('Student')->where($map)->relation('class')->select();
		$this->assign('students', $students);
		$this->display();
	}

	public function download() {
		$map['homework_id'] = I('get.homework_id');
		$map['student_id'] = I('get.student_id');
		$result = M('HomeworkStudent')->where($map)->find();
		if(!$result) {
			$this->error('作业文件不存在！');
		}
		$filename = $result['file_savepath'] . $result['file_savename'];
		$showname = $result['file_name'];
		\Org\Net\Http::download($filename, $showname);
	}

	public function view() {
		$map['homework_id'] = I('get.homework_id');
		$map['student_id'] = I('get.student_id');
		$homeworkStudent = M('HomeworkStudent')->where($map)->find();
		if(!$homeworkStudent) {
			exit('作业不存在！');
		}
		$filename = dirname(__FILE__) . '/../../../' . $homeworkStudent['file_savepath'] . $homeworkStudent['file_savename'];
		$filetype = pathinfo($filename, PATHINFO_EXTENSION);
		$file = null;
		if($filetype == 'doc') {
			$file = curl_file_create($filename,'application/msword','file');
		} else {
			$file = curl_file_create($filename,'application/vnd.openxmlformats-officedocument.wordprocessingml.document','file');
		}
		
		// 调用idocv.com提供的接口得到相应结果
		$url = 'http://api.idocv.com/doc/upload';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
		curl_setopt($ch, CURLOPT_POST, true);
		$data = array(
			'token'	=> 'testtoken',
			'mode'	=> 'public',
			'name'	=> $homeworkStudent['file_savename'],
			'file'	=> $file,
		);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		$result = curl_exec($ch);
		$result = json_decode($result, true);
		curl_close($ch);

		if(!$result['uuid']) {
			exit('空文件！');
		}
		redirect('http://api.idocv.com/view/' . $result['uuid']);
	}

	public function score() {
		if(!IS_POST) {
			$this->redirect('HomeworkStudent/index');
		}
		$rules = array(
			array('homework_id', 'require', '作业id不能为空！'),
			array('student_id', 'require', '学号不能为空！'),
			array('score', 'require', '分数不能为空！'),
			array('score', '0, 100', '分数值有误！', 0, 'between', 3),
		);
		$homeworkStudent = M('HomeworkStudent');
		if(!$homeworkStudent->validate($rules)->create()) {
			$this->error($homeworkStudent->getError());
		}
		$map['homework_id'] = I('post.homework_id');
		$map['student_id'] = I('post.student_id');
		$result = $homeworkStudent->where($map)->find();
		if(!$result) {
			$this->error('作业id和学号有误！');
		}
		$data['id'] = $result['id'];
		$data['homework_id'] = $map['homework_id'];
		$data['student_id'] = $map['student_id'];
		$data['score'] = I('post.score');
		if(!$homeworkStudent->data($data)->save()) {
			$this->error('更新作业学生失败！');
		}
		$this->success('打分成功！');
	}

	public function getHomeworksByLogicClassId() {
		if(!IS_POST) {
			$this->redirect('HomeworkStudent/index');
		}
		$logicClassId = I('post.logic_class_id');
		$homeworks = M('Homework')->where(array('logic_class_id' => $logicClassId))->select();
		$this->ajaxReturn($homeworks);
	}

}
